﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Money;
using OpenSoftStore.Client.Common;

namespace OpenSoftStore.Client.UIL.ManageReport
{
    public partial class ReportMoney : UserControl
    {
        public MoneyLogic moneyBll;
        private static ReportMoney instance;
        public static ReportMoney Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ReportMoney();
                }
                return instance;
            }
        }
        public ReportMoney()
        {
            moneyBll = new MoneyLogic();
            InitializeComponent();
            this.Dock = DockStyle.Fill;

            dgvMoney.AutoGenerateColumns=false;
            dgvMoney.AutoGenerateColumns = false;
            dgvMoney.DataSource = moneyBll.GetMoneyReport(" where AddTime >=#" + DateTime.Now.Date + "#");
            dgvMoneyGroup.AutoGenerateColumns = false;
            dgvMoneyGroup.DataSource = moneyBll.GetMoneyGroupReport(" where T_Money.AddTime >=#" + DateTime.Now.Date + "#");
            SPACommon.SetDataGridStyle(dgvMoney);
            SPACommon.SetDataGridStyle(dgvMoneyGroup);
        }

        private void glassButton1_Click(object sender, EventArgs e)
        {
            DateTime from = dtpFrom.Value.Date;
            DateTime to = dtpTo.Value.Date;
            string whereStr = string.Format(" AddTime between #{0}# and #{1}#  ", from, to);
            //dgvMoney.AutoGenerateColumns = false;
            //dgvMoney.DataSource = moneyBll.GetMoneyReport(whereStr);
            dgvMoney.AutoGenerateColumns = false;
            whereStr = string.Format(" where T_Money.AddTime between #{0}# and #{1}#  ", from, to);
            dgvMoneyGroup.DataSource = moneyBll.GetMoneyGroupReport(whereStr);
        }

        private void dgvMoneyGroup_SelectionChanged(object sender, EventArgs e)
        {
            DateTime from = dtpFrom.Value.Date;
            DateTime to = dtpTo.Value.Date;
     

            if (dgvMoneyGroup.CurrentRow == null) return;
            var treeId = dgvMoneyGroup.CurrentRow.Cells["TreeId"].Value.ToString();

            string whereStr = string.Format(" where  AddTime between #{0}# and #{1}# and TreeId = '{2}'", from, to, treeId);

            dgvMoney.AutoGenerateColumns = false;
            dgvMoney.DataSource = moneyBll.GetMoneyReport(whereStr);
        }

        private void btnOutput_Click(object sender, EventArgs e)
        {
            SPACommon.ExportToExcel(dgvMoneyGroup, "收支统计");
        }
    }
}
